const gulp = require('gulp'),
	sass = require('gulp-sass'),
	uglify = require('gulp-uglify'),
	autoprefixer = require('gulp-autoprefixer'),
	livereload = require('gulp-livereload'),
	notify = require('gulp-notify'),
	cached = require('gulp-cached'),
	clean = require('gulp-clean');

// 注意: gulp-sass 不要升级, 不然会报如下错
// Error: resources/assets/scss/vendor/bootstrap/mixins/_buttons.scss
// Error: Base-level rules cannot contain the parent-selector-referencing character '&'.


const autoprefixerConfig = {
	browsers: ['last 5 versions']
};

const sassConfig = {
	// outputStyle: 'expanded'
	outputStyle: 'compressed'
};

const uglifyConfig = {
	// mangle: false
};

const sassBlobs = {
    web: {
        src: 'resources/assets/scss/web/**/*.scss',
        dist: 'public/css/web',
    },
    admin: {
        src: 'resources/assets/scss/admin/**/*.scss',
        dist: 'public/css/admin',
    },
};

const jsBlobs = {
    web: {
        src: 'resources/assets/js/web/**/*.js',
        dist: 'public/js/web',
    },
    admin: {
        src: 'resources/assets/js/admin/**/*.js',
        dist: 'public/js/admin',
    },
};

const sassErrorCb = function(error) {
    console.log('========================');
    console.log('文件:', error.file);
    console.log('  行:', error.line);
    console.log('  列:', error.column);
    console.log('错误:', error.message);
    console.log('========================');
};

const jsErrorCb = function(error) {
    console.log('========================');
    console.log('文件:', error.fileName);
    console.log('  行:', error.cause.line);
    console.log('  列:', error.cause.col);
    console.log('错误:', error.cause.message);
    console.log('========================');
};

// 默认任务
gulp.task('default', function () {
    console.log('查看: gulpfile.js  :P');
});


/**
 * web
 * ================================================
 */

// 编译 sass
gulp.task('sass:web', function () {
	gulp.src(sassBlobs.web.src)
		//.pipe(cached('sass')) // 不稳定时注释掉
		.pipe(sass(sassConfig))
		.on("error", notify.onError("Error: <%= error.message %>"))
		.on('error', sassErrorCb)
		.pipe(autoprefixer(autoprefixerConfig))
		.pipe(gulp.dest(sassBlobs.web.dist))
		.pipe(livereload());
});

// 混淆压缩 js
gulp.task('js:web', function () {
	gulp.src(jsBlobs.web.src)
		.pipe(cached('js'))
		.pipe(uglify(uglifyConfig))   // 压缩混淆 js
		.on("error", notify.onError("Error: <%= error.message %>"))
		.on('error', jsErrorCb)
		.pipe(gulp.dest(jsBlobs.web.dist))
		.pipe(livereload());
});

// 文件有改动就编译
gulp.task('watch:web', function () {

	gulp.start('sass:web');
	gulp.start('js:web');

	livereload.listen();

	gulp.watch(sassBlobs.web.src, [
		'sass:web'
	]);

	gulp.watch(jsBlobs.web.src, [
		'js.web'
	]);
});

// 删除编译后的 js, css
gulp.task('clean:web', function () {
	return gulp.src([
			sassBlobs.web.dist,
        	jsBlobs.web.dist,
		], {read: false})
		.pipe(clean({force: true}));
});

/**
 * admin
 * ================================================
 */

// 编译 sass
gulp.task('sass:admin', function () {
    gulp.src(sassBlobs.admin.src)
    //.pipe(cached('sass')) // 不稳定时注释掉
        .pipe(sass(sassConfig))
        .on("error", notify.onError("Error: <%= error.message %>"))
        .on('error', sassErrorCb)
        .pipe(autoprefixer(autoprefixerConfig))
        .pipe(gulp.dest(sassBlobs.admin.dist))
        .pipe(livereload());
});

// 混淆压缩 js
gulp.task('js:admin', function () {
    gulp.src(jsBlobs.admin.src)
        .pipe(cached('js'))
        .pipe(uglify(uglifyConfig))   // 压缩混淆 js
        .on("error", notify.onError("Error: <%= error.message %>"))
        .on('error', jsErrorCb)
        .pipe(gulp.dest(jsBlobs.admin.dist))
        .pipe(livereload());
});

// 文件有改动就编译
gulp.task('watch:admin', function () {

    gulp.start('sass:admin');
    gulp.start('js:admin');

    livereload.listen();

    gulp.watch(sassBlobs.admin.src, [
        'sass:admin'
    ]);

    gulp.watch(jsBlobs.admin.src, [
        'js.admin'
    ]);
});

// 删除编译后的 js, css
gulp.task('clean:admin', function () {
    return gulp.src([
        sassBlobs.admin.dist,
        jsBlobs.admin.dist,
    ], {read: false})
        .pipe(clean({force: true}));
});


